package com.hx0311.core.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;

import com.hx0311.core.dao.support.ResultSetUtil;
import com.hx0311.core.datasource.DataSourceManager;

/**
 * 数据操作的基类,获得链接,关闭链接
 * 
 * @author Lunch
 */
public abstract class BaseDao {

	public abstract String getTableName();

	public abstract String getIdName();

	/**
	 * 获得数据库链接
	 * 
	 * @return
	 * @throws SQLException
	 */
	protected Connection getConnection() throws SQLException {
		return DataSourceManager.getConnection();
	}

	/**
	 * 关闭链接
	 * 
	 * @param conn
	 */
	protected void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭Statement
	 * 
	 * @param conn
	 */
	protected void close(Statement statm) {
		if (statm != null) {
			try {
				statm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭结果集
	 * 
	 * @param rs
	 */
	protected void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	protected void close(Statement statm, Connection conn) {
		close(statm);
		close(conn);
	}

	protected void close(ResultSet rs, Statement statm, Connection conn) {
		close(rs);
		close(statm);
		close(conn);
	}

	/**
	 * 根据主键删除一条记录
	 * 
	 * @param idValue
	 *            主键值
	 */
	public void remove(String idValue) {
		if (StringUtils.isNotBlank(getTableName())
				&& StringUtils.isNotBlank(getIdName())
				&& StringUtils.isNotBlank(idValue)) {

			StringBuffer sql = new StringBuffer("delete from ");
			sql.append(getTableName()).append(" where ").append(getIdName());
			sql.append(" = ?");

			Connection conn = null;
			PreparedStatement stat = null;
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql.toString());
				stat.setString(1, idValue);
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				close(stat, conn);
			}
		}
	}

	/**
	 * 根据主键获得条记录
	 * 
	 * @param tableName
	 * @param colName
	 * @param colValue
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map get(String idValue) {
		if (StringUtils.isNotBlank(getTableName())
				&& StringUtils.isNotBlank(getIdName())
				&& StringUtils.isNotBlank(idValue)) {
			StringBuffer sql = new StringBuffer("select * from ");
			sql.append(getTableName()).append(" where ").append(getIdName());
			sql.append(" = ?");
			Connection conn = null;
			PreparedStatement stat = null;
			ResultSet rs = null;
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql.toString());
				stat.setString(1, idValue);
				rs = stat.executeQuery();
				if (rs.next()) {
					return ResultSetUtil.toMap(rs, rs.getMetaData());
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				close(rs, stat, conn);
			}
		}
		return Collections.EMPTY_MAP;
	}

}
